package com.getinstaliker.screen;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.getinstaliker.activity.MainFragmentActivity;
import com.getinstaliker.instagram.InstagramBaseTask.InstagramTaskListener;
import com.getinstaliker.instagram.InstagramUtil;
import com.getinstaliker.instagram.model.MediaImageModel.ImageData;
import com.getinstaliker.instagram.model.MediaImageModel.ImageInfo;
import com.getinstaliker.instagram.model.MediaImageModel.MediaData;
import com.getinstaliker.instagram.model.UserMediaModel;
import com.getinstaliker.screen.common.BaseGetInstalikerHeaderFragment;
import com.getinstaliker.util.UniversalImageLoader;
import com.instagram.getinstaliker.R;

// TODO: Auto-generated Javadoc
/**
 * The Class GetLikesFragment.
 */
public class GetLikesFragment extends BaseGetInstalikerHeaderFragment implements OnItemClickListener {

	/** The spend frag. */
	private SpendFragment spendFrag;
	
	/** The m grid photos. */
	private GridView mGridPhotos;
	
	/** The m list data. */
	private UserMediaModel mListData;
	
	/** The m adapter. */
	private GridPhotoAdapter mAdapter;
	
	/** The Constant NUM_PHOTO_COLUMN. */
	public static final int NUM_PHOTO_COLUMN = 2;
	
	/* (non-Javadoc)
	 * @see com.getinstaliker.screen.common.BaseFragmentScreen#onCreateContentView(android.view.LayoutInflater, android.view.ViewGroup)
	 */
	@Override
	protected View onCreateContentView(LayoutInflater inflater,
			ViewGroup container) {
		View vContent = inflater.inflate(R.layout.get_likes_screen, container, false);
		mGridPhotos = (GridView) vContent.findViewById(R.id.gridGetLikes);
		mGridPhotos.setOnItemClickListener(this);
		mGridPhotos.setNumColumns(NUM_PHOTO_COLUMN);
		loadData();
		return vContent;
	}
	

	/**
	 * Load data.
	 */
	public void loadData(){
		InstagramUtil.getUserPhotosTask(getActivity(), new InstagramTaskListener<UserMediaModel>() {
			
			@Override
			public void onTaskComplete(UserMediaModel ob) {
				if(ob!=null){
					if(mListData!=null){
						mListData.data.clear();
						mAdapter.notifyDataSetChanged();
					}
					mListData = ob;
					if(mAdapter==null){
						mAdapter = new GridPhotoAdapter();
						mGridPhotos.setAdapter(mAdapter);
					}else{
						mAdapter.notifyDataSetChanged();
					}
				}
				
			}
		});
	}
	
	/* (non-Javadoc)
	 * @see com.getinstaliker.screen.common.BaseGetInstalikerHeaderFragment#onReload()
	 */
	@Override
	public void onReload() {
		loadData();
	}
	
	/**
	 * Show detail.
	 *
	 * @param pos the pos
	 */
	public void showDetail(int pos){
		MediaData data = mListData.data.get(pos);
		if(spendFrag==null){
			spendFrag = new SpendFragment(data);
			addChildFragment(spendFrag,true);
		}else{
			showChildFragment(spendFrag);
			spendFrag.loadData(data);
		}
	}
	
	/* (non-Javadoc)
	 * @see com.getinstaliker.screen.common.BaseGetInstalikerHeaderFragment#getTitleResId()
	 */
	@Override
	protected int getTitleResId() {
		return R.string.get_likes;
	}

	/* (non-Javadoc)
	 * @see android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget.AdapterView, android.view.View, int, long)
	 */
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		showDetail(arg2);
	}

	/**
	 * The Class GridPhotoAdapter.
	 */
	public class GridPhotoAdapter extends BaseAdapter{
		
		/** The image size. */
		private final int IMAGE_SIZE = (int) (MainFragmentActivity.SCREEN_WIDTH/3);
		
		/* (non-Javadoc)
		 * @see android.widget.Adapter#getCount()
		 */
		@Override
		public int getCount() {
			if(mListData!=null){
				if(mListData.data!=null){
					return mListData.data.size();
				}
			}
			return 0;
		}

		/* (non-Javadoc)
		 * @see android.widget.Adapter#getItem(int)
		 */
		@Override
		public Object getItem(int arg0) {
			return null;
		}

		/* (non-Javadoc)
		 * @see android.widget.Adapter#getItemId(int)
		 */
		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		/* (non-Javadoc)
		 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
		 */
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			HolderView holder = null;
			if(convertView==null){
				convertView = LayoutInflater.from(getActivity()).inflate(R.layout.grid_item, null);
				holder = new HolderView();
				LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(IMAGE_SIZE, IMAGE_SIZE);
				holder.imgPhoto = (ImageView) convertView.findViewById(R.id.imgPhoto);
				holder.imgPhoto.setLayoutParams(layoutParams);
				holder.tvInfo = (TextView) convertView.findViewById(R.id.tvInfo);
				convertView.setTag(holder);
			}else{
				holder = (HolderView) convertView.getTag();
			}
			
			ImageInfo imageInfo = chooseResolution(mListData.data.get(position).images);
			holder.tvInfo.setText(String.valueOf(mListData.data.get(position).likes.count)+" likes");
			UniversalImageLoader.getInstance().loadImageView(holder.imgPhoto, imageInfo.url, IMAGE_SIZE, IMAGE_SIZE,false);
			return convertView;
		}
		
		/**
		 * The Class HolderView.
		 */
		public class HolderView{
			
			/** The img photo. */
			public ImageView imgPhoto;
			
			/** The tv info. */
			public TextView  tvInfo;
		}
		
		/**
		 * Choose resolution.
		 *
		 * @param imageData the image data
		 * @return the image info
		 */
		public ImageInfo chooseResolution(ImageData imageData){
			if(Integer.valueOf(imageData.standard_resolution.width)<IMAGE_SIZE){
				return imageData.standard_resolution;
			}else if(Integer.valueOf(imageData.low_resolution.width)<IMAGE_SIZE){
				return imageData.low_resolution;
			}else{
				return imageData.thumbnail;
			}
		}
		
	}
	

}
